package com.ynny.test;

import com.ynny.bean.Score;
import com.ynny.bean.User;
import com.ynny.dao.CourseManage;
import com.ynny.dao.ScoreManage;
import com.ynny.dao.UserManage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class StudentSystemDB {
    public static void main(String[] args) {
        ApplicationContext context = null;
        new ClassPathXmlApplicationContext("applicationContex.xml");
        User user = (User) context.getBean("user");
        UserManage userManage = (UserManage) context.getBean("userManage");
        Score score = (Score) context.getBean("score");

        ScoreManage scoreManage = (ScoreManage) context.getBean("scoreManage");
        CourseManage courseManage = (CourseManage) context.getBean("courseManage");


        user.setNumber("11015");
        user.setPassword("654321");
        user.setIdentity("教师");
        if (userManage.login(user)) {
            System.out.println(user.getIdentity() + "用户登录成功");
            System.out.println("插入20220103001的学生成绩记录");
            score.setNumber("20220103001");
            score.setChinese(89.0);
            score.setEnglish(100.0);
            score.setMath(100.0);
            scoreManage.addScore(score);
            System.out.println("查询所有的学生成绩记录:");
            scoreManage.queryAllScore();
        } else {
            System.out.println(user.getIdentity() + "账号或密码错误");

            user.setNumber("20220103001");
            user.setPassword("123456");
            user.setIdentity("学生");
            if (userManage.login(user)) {
                System.out.println(user.getIdentity() + "用户登录成功");
                System.out.println("查询20220103001学生的成绩记录");
                scoreManage.queryScoreByNum("20220103001");
                System.out.println("查询待选课记录:");
                courseManage.queryCourse();
                try {
                    System.out.println("执行选课操作");
                    courseManage.selectCourse("20110102001", "moive");
                } catch (RuntimeException e) {
                    System.out.println("异常信息:" + e.getMessage());
                    System.out.println("事务执行回滚");
                } finally {
                    System.out.println("执行选课操作后，查询待选课程记录");
                    courseManage.queryCourse();
                }
            } else
                System.out.println(user.getIdentity() + "用户账号或密码错误");
            ((AbstractApplicationContext) context).close();
        }
    }
}
